﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;

public partial class Main_Master_ReminderView : System.Web.UI.Page
{
    Datautility DU = new Datautility();
    string strsql = "";
    DataTable DTMain = new DataTable();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userid"] == null)
        {
            Response.Redirect("~/SessionExpired.aspx");
        }
        if (!IsPostBack)
        {
            //**************Set Minimum and maximum date calender Control********
          //  AccountSettings.setMinMaxDate(form1);
            //********************End **********
            string finyear = Session["finyear"].ToString();
            txtFrom.Text = "01/04/" + finyear;
            txtTo.Text = "31/03/" + (Convert.ToInt32(finyear) + 1).ToString();
            BindCustomData();
        }
    }
    protected void BindCustomData()
    {
        if (txtFrom.Text != string.Empty && txtTo.Text != string.Empty)
        {
        strsql = "select * from MAS_REMINDER where CDATE between '"+string.Format("{0:MM/dd/yyyy}",Convert.ToDateTime(txtFrom.Text))+"' and '"+string.Format("{0:MM/dd/yyyy}",Convert.ToDateTime(txtTo.Text))+"'";
        DTMain = DU.getdata(strsql);
        }

        grdDisplay.DataSource = DTMain;
        grdDisplay.DataBind();
        if (DTMain.Rows.Count <= 0)
        {
            custPager.Visible = false;
            //CommonControls2.Visible = false;
        }
        else
        {
            custPager.Visible = true;
            //CommonControls2.Visible = true;
        }
    }

    protected void grdDisplay_OnPageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdDisplay.PageIndex = e.NewPageIndex;
        BindCustomData();
    }

   
    protected void grdDisplay_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        strsql = "select LID,LNAME from ACC_LEDMAST";
        DataTable dtLedger = DU.getdata(strsql);
        DataView dvLedger = new DataView();
        if (dtLedger.Rows.Count > 0)
        {
            dvLedger = dtLedger.DefaultView;
        }

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //GridView grdViewReminder = (GridView)e.Row.FindControl("grdViewReminder");
            //string strReceiptId = grdDisplay.DataKeys[e.Row.RowIndex].Value.ToString();
            //strsql = "select * from MAS_REMINDER where id=" + strReceiptId;
            //DataTable dt = DU.getdata(strsql);
            //grdViewReminder.DataSource = dt;
            //grdViewReminder.DataBind();

            int VouTypeIndexNo = 0;
            bool ISACTIVE = true;
            int frequency = 0;

            Label lblVouType = (Label)e.Row.FindControl("lblVouType");
            Label lblRemDate = (Label)e.Row.FindControl("lblRemDate");
            Label lblDebitLedger = (Label)e.Row.FindControl("lblDebitLedger");
            Label lblCreditLedger=(Label)e.Row.FindControl("lblCreditLedger");
            Label lblAlertBeforeDays = (Label)e.Row.FindControl("lblAlertBeforeDays");
            Label lblIsActive = (Label)e.Row.FindControl("lblIsActive");
            Label lblFrequency = (Label)e.Row.FindControl("lblFrequency");
            Label lblRemarks = (Label)e.Row.FindControl("lblRemarks");
            Label lblActivateFrom = (Label)e.Row.FindControl("lblActivateFrom");
            Label lblCompName = (Label)e.Row.FindControl("lblCompName");

            if (Convert.ToString(DataBinder.Eval(e.Row.DataItem, "type")) != string.Empty)
            {
                VouTypeIndexNo = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "type"));
            }
            string DebitLID = DataBinder.Eval(e.Row.DataItem, "DEBIT_LID").ToString();
            string CreditLID = DataBinder.Eval(e.Row.DataItem, "CREDIT_LID").ToString();
            if (Convert.ToString(DataBinder.Eval(e.Row.DataItem, "ISACTIVE")) != string.Empty)
            {
                ISACTIVE = Convert.ToBoolean(DataBinder.Eval(e.Row.DataItem, "ISACTIVE"));
            }
            if (Convert.ToString(DataBinder.Eval(e.Row.DataItem, "Date")) != string.Empty)
            {
                lblRemDate.Text = string.Format("{0:dd-MMM-yyyy}", Convert.ToDateTime(DataBinder.Eval(e.Row.DataItem, "Date")));
            }
            lblAlertBeforeDays.Text = DataBinder.Eval(e.Row.DataItem, "ALERT_BEFORE_DAYS").ToString();
            if (Convert.ToString(DataBinder.Eval(e.Row.DataItem, "ACTIVATE_FROM_DATE")) != string.Empty)
            {
                lblActivateFrom.Text = string.Format("{0:dd-MMM-yyyy}", Convert.ToDateTime(DataBinder.Eval(e.Row.DataItem, "ACTIVATE_FROM_DATE")));
            }
            if (Convert.ToString(DataBinder.Eval(e.Row.DataItem, "FREQUENCY")) != string.Empty)
            {
                frequency = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "FREQUENCY"));
            }

            lblRemarks.Text = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "REMARKS"));
            lblCompName.Text = Session["compname"].ToString();

            if (VouTypeIndexNo == 1)
            { 
                e.Row.Cells[1].Text="Contra";
                lblVouType.Text = "Contra";
            }
            else if (VouTypeIndexNo == 2)
            { 
                e.Row.Cells[1].Text="Credit Note";
                lblVouType.Text = "Credit Note";
            }
            else if (VouTypeIndexNo == 3)
            { 
                e.Row.Cells[1].Text="Debit Note";
                lblVouType.Text = "Debit Note";
            }
            else if (VouTypeIndexNo == 4)
            { 
                e.Row.Cells[1].Text="Journal";
                lblVouType.Text = "Journal";
            }
            else if (VouTypeIndexNo == 5)
            { 
                e.Row.Cells[1].Text="Payment";
                lblVouType.Text = "Payment";
            }
            else if (VouTypeIndexNo == 6)
            { 
                e.Row.Cells[1].Text="Purchase";
                lblVouType.Text = "Purchase";
            }
            else if (VouTypeIndexNo == 7)
            { 
                e.Row.Cells[1].Text="Purchase return";
                lblVouType.Text = "Purchase return";
            }
            else if (VouTypeIndexNo == 8)
            { 
                e.Row.Cells[1].Text="Receipt";
                lblVouType.Text = "Receipt";
            }
            else if (VouTypeIndexNo == 9)
            { 
                e.Row.Cells[1].Text="Sales";
                lblVouType.Text = "Sales";
            }
            else if (VouTypeIndexNo == 10)
            {
                e.Row.Cells[1].Text = "Sales return";
                lblVouType.Text = "Sales return";
            }
            if (ISACTIVE == true)
            {
                e.Row.Cells[5].Text = "Active";
                lblIsActive.Text = "Active";
            }
            else
            {
                e.Row.Cells[5].Text = "Inactive";
                lblIsActive.Text = "Inactive";
            }
            string strExprDebit = "LID=" + DebitLID;
            string strExprCredit = "LID=" + CreditLID;

            dvLedger.RowFilter = strExprCredit;
            DataTable dtCreditLedger=dvLedger.ToTable();
            if (dtCreditLedger.Rows[0]["LNAME"].ToString() != string.Empty)
            {
                e.Row.Cells[4].Text = dtCreditLedger.Rows[0]["LNAME"].ToString();
                lblCreditLedger.Text = dtCreditLedger.Rows[0]["LNAME"].ToString();
            }

            dvLedger.RowFilter = strExprDebit;
            DataTable dtDebitLedger = dvLedger.ToTable();
            if (dtDebitLedger.Rows[0]["LNAME"].ToString() != string.Empty)
            {
                e.Row.Cells[3].Text = dtDebitLedger.Rows[0]["LNAME"].ToString();
                lblDebitLedger.Text = dtDebitLedger.Rows[0]["LNAME"].ToString();
            }
            if (frequency == 1)
            {
                lblFrequency.Text = "Daily";
            }
            else if (frequency == 2)
            {
                lblFrequency.Text = "Weekly";
            }
            else if (frequency == 3)
            {
                lblFrequency.Text = "Half Monthly";
            }
            else if (frequency == 4)
            {
                lblFrequency.Text = "Monthly";
            }
        }
    }
    protected void custPager_PageChanged(object sender, CustomPageChangeArgs e)
    {
        grdDisplay.PageSize = e.CurrentPageSize;
        grdDisplay.PageIndex = e.CurrentPageNumber;
        BindCustomData();
    }
    protected void grdDisplay_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType != DataControlRowType.Pager)
        {
            if (e.Row.RowState == DataControlRowState.Alternate)
            {
                e.Row.Attributes.Add("onmouseover", "this.className='rowhover'");
                e.Row.Attributes.Add("onmouseout", "this.className='gridview_alternate'");
                //e.Row.Attributes.Add("onclick", "this.className='rowselected'");
            }
            else if (e.Row.RowState == DataControlRowState.Normal)
            {
                e.Row.Attributes.Add("onmouseover", "this.className='rowhover'");
                e.Row.Attributes.Add("onmouseout", "this.className='gridviewitem'");
                //e.Row.Attributes.Add("onclick", "this.className='rowselected'");
            }
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (txtFrom.Text != string.Empty && txtTo.Text != string.Empty)
        {
            BindCustomData();
        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Welcome.aspx");
    }

    //protected void grdViewReminder_RowDataBound(object sender, GridViewRowEventArgs e)
    //{
    //    strsql = "select LID,LNAME from ACC_LEDMAST";
    //    DataTable dtLedger = DU.getdata(strsql);
    //    DataView dvLedger = new DataView();
    //    if (dtLedger.Rows.Count > 0)
    //    {
    //        dvLedger = dtLedger.DefaultView;
    //    }

    //    if (e.Row.RowType == DataControlRowType.DataRow)
    //    {
    //        int VouTypeIndexNo = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "type"));
    //        string DebitLID = DataBinder.Eval(e.Row.DataItem, "DEBIT_LID").ToString();
    //        string CreditLID = DataBinder.Eval(e.Row.DataItem, "CREDIT_LID").ToString();
    //        bool ISACTIVE = Convert.ToBoolean(DataBinder.Eval(e.Row.DataItem, "ISACTIVE"));

    //        if (VouTypeIndexNo == 1)
    //        {
    //            e.Row.Cells[1].Text = "Contra";
    //        }
    //        else if (VouTypeIndexNo == 2)
    //        {
    //            e.Row.Cells[1].Text = "Credit Note";
    //        }
    //        else if (VouTypeIndexNo == 3)
    //        {
    //            e.Row.Cells[1].Text = "Debit Note";
    //        }
    //        else if (VouTypeIndexNo == 4)
    //        {
    //            e.Row.Cells[1].Text = "Journal";
    //        }
    //        else if (VouTypeIndexNo == 5)
    //        {
    //            e.Row.Cells[1].Text = "Payment";
    //        }
    //        else if (VouTypeIndexNo == 6)
    //        {
    //            e.Row.Cells[1].Text = "Purchase";
    //        }
    //        else if (VouTypeIndexNo == 7)
    //        {
    //            e.Row.Cells[1].Text = "Purchase return";
    //        }
    //        else if (VouTypeIndexNo == 8)
    //        {
    //            e.Row.Cells[1].Text = "Receipt";
    //        }
    //        else if (VouTypeIndexNo == 9)
    //        {
    //            e.Row.Cells[1].Text = "Sales";
    //        }
    //        else if (VouTypeIndexNo == 10)
    //        {
    //            e.Row.Cells[1].Text = "Sales return";
    //        }
    //        if (ISACTIVE == true)
    //        {
    //            e.Row.Cells[6].Text = "Active";
    //        }
    //        else
    //        {
    //            e.Row.Cells[6].Text = "Inactive";
    //        }
    //        string strExprDebit = "LID=" + DebitLID;
    //        string strExprCredit = "LID=" + CreditLID;

    //        dvLedger.RowFilter = strExprCredit;
    //        DataTable dtCreditLedger = dvLedger.ToTable();
    //        if (dtCreditLedger.Rows[0]["LNAME"].ToString() != string.Empty)
    //        {
    //            e.Row.Cells[4].Text = dtCreditLedger.Rows[0]["LNAME"].ToString();
    //        }

    //        dvLedger.RowFilter = strExprDebit;
    //        DataTable dtDebitLedger = dvLedger.ToTable();
    //        if (dtDebitLedger.Rows[0]["LNAME"].ToString() != string.Empty)
    //        {
    //            e.Row.Cells[3].Text = dtDebitLedger.Rows[0]["LNAME"].ToString();
    //        }
    //    }
    //}

}
